package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

//import com.xx.pmis.model.Item;
import com.xx.pmis.model.Account;
import com.xx.pmis.model.Item;
import com.xx.pmis.model.util.PresentMonth;
import com.xx.pmis.services.ItemService;
import com.xx.pmis.web.forms.ItemForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.ItemUtil;
import com.xx.pmis.web.util.PresentUtil;

/**
 * @class 事项Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class ItemAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(ItemAction.class);

	ItemService itemService = getPMISServices().getItemService();
	ItemUtil itemUtil = new ItemUtil();
	AccountUtil accountUtil = new AccountUtil();
	PresentUtil presentUtil = new PresentUtil();

//	// 查询所有待办事项
//	public ActionForward listWaitingItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//			HttpServletResponse response) {
//
//		List<Item> all = null;
//		try {
//			all = itemUtil.getAllWaitingItem();
//		} catch (Exception e) {
//			String errorPage = "【日常事务/待办事项】查询【待办事项】异常！" + e.toString();
//			log.info(errorPage);
//			request.setAttribute("errorPage", errorPage);
//			request.setAttribute("position", "出错了");
//			return mapping.findForward("errorPage");
//		}
//		if (all.size() == 0) {
//			request.setAttribute("error", "最近没有待办事项，需要添加吗？");
//		} else {
//			request.setAttribute("error", "共有 " + all.size() + " 条待办事项，请及时处理。");
//		}
//		request.setAttribute("position", "日常事务/待办事项");
//		request.setAttribute("itemWaitingList", all);
//		return mapping.findForward("listWaitingItem");
//	}
	
	// 查询所有待办事项
	public ActionForward listWaitingItemJump(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		return mapping.findForward("listWaitingItemJump");
	}
	
	// 查询所有待办事项
	public ActionForward listWaitingItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<PresentMonth> presentMonthList = null;
		try {
			presentMonthList = presentUtil.getPresentOfTwelveMonths();
		} catch (Exception e) {
			String errorPage = "【物资管理/商品】【最近十二个月的购买商品支出汇总】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("presentMonthList", presentMonthList);
		List<Account> accountList = accountUtil.getReductionAndIncreaseOfTwelveMonths();

		request.setAttribute("accountList", accountList);
		request.setAttribute("position", "日常事务/待办事项");
		return mapping.findForward("listWaitingItem");
	}

	// 查询所有完成事项
	public ActionForward listAccomplishItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Item> all = null;
		try {
			all = itemService.getAllAccomplishItem();
		} catch (Exception e) {
			String errorPage = "【日常事务/完成事项】查询【完成事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("position", "日常事务/完成事项");
		request.setAttribute("itemAccomplishList", all);
		return mapping.findForward("listAccomplishItem");
	}

	// 新增
	public ActionForward addItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		// request.setAttribute("ItemDate", CurrentDate.currentDate());
		request.setAttribute("position", "待办事项/新增");
		request.setAttribute("itemTime", CurrentDate.currentDate() + " 18:00");
		return mapping.findForward("addItem");
	}

	// 新增，处理方法
	public ActionForward addItem_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ItemForm f = (ItemForm) form;

		String itemName = f.getItemName(); // 事项名称
		String itemTime = f.getItemTime(); // 最迟处理时间
		String itemCompany = f.getItemCompany(); // 关系人/单位
		String itemDesc = f.getItemDesc(); // 描述

		Item item = new Item();
		item.setItemName(itemName);
		item.setItemTime(itemTime);
		item.setItemCompany(itemCompany);
		item.setItemState("1");
		item.setItemDesc(itemDesc);
		try {
			itemService.saveOrUpdateItem(item);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/新增】保存或更新【待办事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		this.listWaitingItem(mapping, form, request, response);
		return mapping.findForward("listWaitingItem");
	}

	// 编辑
	public ActionForward editItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String itemID = request.getParameter("itemID");
		Item item = new Item();
		try {
			item = itemService.getItemByItemID(itemID);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/编辑】根据【待办事项ID】查询该【待办事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("position", "待办事项/编辑");
		request.setAttribute("item", item);
		return mapping.findForward("editItem");
	}

	// 编辑，处理方法
	public ActionForward editItem_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ItemForm f = (ItemForm) form;

		String ItemID = f.getItemID();
		String itemName = f.getItemName(); // 事项名称
		String itemTime = f.getItemTime(); // 最迟处理时间
		String itemCompany = f.getItemCompany(); // 关系人/单位
		String itemDesc = f.getItemDesc(); // 描述

		Item item = null;
		try {
			item = itemService.getItemByItemID(ItemID);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/编辑】根据【待办事项ID】查询该【待办事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		item.setItemName(itemName);
		item.setItemTime(itemTime);
		item.setItemCompany(itemCompany);
		item.setItemDesc(itemDesc);
		try {
			itemService.saveOrUpdateItem(item);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/编辑】保存或更新【待办事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		this.listWaitingItem(mapping, form, request, response);
		return mapping.findForward("listWaitingItem");
	}

	// 删除
	public ActionForward delItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Item item = null;
		ItemForm f = (ItemForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				item = itemService.getItemByItemID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【日常事务/待办事项/删除】根据【待办事项ID】查询该【待办事项】异常！异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				itemService.deleteItem(item);
			} catch (Exception e) {
				String errorPage = "【日常事务/待办事项/删除】删除【待办事项】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		this.listWaitingItem(mapping, form, request, response);
		return mapping.findForward("listWaitingItem");
	}

	// 完成
	public ActionForward accomplishItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String itemID = request.getParameter("itemID");

		Item item = null;
		try {
			item = itemService.getItemByItemID(itemID);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/完成】根据【待办事项ID】查询该【待办事项】异常！异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		// 完成日期
		item.setItemDate(CurrentDate.currentDate());
		item.setItemState("0");

		try {
			itemService.saveOrUpdateItem(item);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/完成】保存或更新【待办事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		this.listAccomplishItemByPage(mapping, form, request, response);
		return mapping.findForward("listAccomplishItem");
	}

	// 明细
	public ActionForward detailItem(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String itemID = request.getParameter("itemID");

		Item item = null;
		try {
			item = itemService.getItemByItemID(itemID);
		} catch (Exception e) {
			String errorPage = "【日常事务/待办事项/明细】根据【待办事项ID】查询该【待办事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("item", item);
		request.setAttribute("position", "事项/明细");
		return mapping.findForward("detailItem");
	}
	
	// 分页显示
	public ActionForward listAccomplishItemByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = itemService.getCountOfAccomplishItem();
		} catch (Exception e) {
			String errorPage = "【日常事务/已办事项】查询【已办事项】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}

		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<?> all = null;
		try {
			all = itemService.getAllAccomplishItemByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【日常事务/完成事项】查询【完成事项】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("itemAccomplishList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}
		
		if(totalPage == 0) {
			request.setAttribute("pageNum", "0");
			request.setAttribute("noUp", "yes");
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "item.do");
		request.setAttribute("position", "事项管理/完成事项");
		return mapping.findForward("listAccomplishItem");
	}

}